public class ExcelToImageConverter implements Converter {
    @Override
    public boolean support(FileFormat sourceFormat, FileFormat targetFormat) {
        return (sourceFormat == FileFormat.XLSX || sourceFormat == FileFormat.XLS) 
                && (targetFormat == FileFormat.PNG || targetFormat == FileFormat.JPG);
    }
    
    @Override
    public void convert(File source, File target, ConversionConfig config) throws ConversionException {
        try {
            // 使用POI加载Excel
            Workbook workbook;
            if (source.getName().endsWith("xlsx")) {
                workbook = new XSSFWorkbook(source);
            } else {
                workbook = new HSSFWorkbook(new FileInputStream(source));
            }
            
            Sheet sheet = workbook.getSheetAt(0);
            
            // 计算实际内容的范围
            int maxRow = sheet.getLastRowNum();
            int maxCol = 0;
            for (Row row : sheet) {
                maxCol = Math.max(maxCol, row.getLastCellNum());
            }
            
            // 设置单元格的默认宽高
            int cellWidth = 100;
            int cellHeight = 25;
            int width = maxCol * cellWidth;
            int height = (maxRow + 1) * cellHeight;
            
            // 创建图片
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = image.createGraphics();
            
            // 设置背景
            graphics.setColor(Color.WHITE);
            graphics.fillRect(0, 0, width, height);
            
            // 设置字体
            graphics.setFont(new Font("Arial", Font.PLAIN, 10));
            graphics.setColor(Color.BLACK);
            
            // 绘制单元格内容
            for (int i = 0; i <= maxRow; i++) {
                Row row = sheet.getRow(i);
                if (row != null) {
                    for (int j = 0; j < maxCol; j++) {
                        Cell cell = row.getCell(j);
                        if (cell != null) {
                            String content = getCellContent(cell);
                            int x = j * cellWidth + 5;
                            int y = i * cellHeight + 15;
                            graphics.drawString(content, x, y);
                            
                            // 绘制单元格边框
                            graphics.drawRect(j * cellWidth, i * cellHeight, cellWidth, cellHeight);
                        }
                    }
                }
            }
            
            graphics.dispose();
            
            // 保存图片
            String formatName = target.getName().substring(target.getName().lastIndexOf('.') + 1);
            ImageIO.write(image, formatName, target);
            
            workbook.close();
        } catch (IOException e) {
            throw new FileOperationException("Excel转图片失败", e);
        }
    }
    
    private String getCellContent(Cell cell) {
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    return cell.getDateCellValue().toString();
                }
                return String.valueOf(cell.getNumericCellValue());
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case FORMULA:
                return cell.getCellFormula();
            default:
                return "";
        }
    }
} 